import { Wujiang } from "./u";
import zhanfaList from "./zhanfa.json";

// 随机数 0-1之间
export const random = (num: number) => {
  return Math.random() < num;
};

// 获取战法详情
export const getZhanfa = (name: string) => {
  return zhanfaList.find((item) => item.name === name);
};

// 攻击距离内的敌人
export const getEnemy = (u: Wujiang, uList: Wujiang[]) => {
  const index = uList.findIndex((item) => item === u);
  return uList.filter((item, i) => {
    return item.lr !== u.lr && u.distance >= Math.abs(index - i);
  });
};

// 战法距离内
export const getEnemyByZhanfa = (zhanBao: string[], u: Wujiang, uList: Wujiang[], zhanfaJuli: number, baoKuoZisheng = false, youjun = false) => {
  const index = uList.findIndex((item) => item === u);
  let isBaozou = false;
  if (u.zhuangtai.baozou.huihe && random(u.zhuangtai.baozou.jilv)) {
    zhanBao.push(`<span class="${u.lr}">【${u.name}】</span>受到战法【${u.zhuangtai.baozou.laiyuanZhanfa}】影响,进入暴走状态,不分敌我`);
    isBaozou = true;
  }
  return uList.filter((item, i) => {
    if (isBaozou) {
      return zhanfaJuli >= Math.abs(index - i) && item !== u;
    } else {
      if (baoKuoZisheng) {
        if (youjun) {
          return item.lr === u.lr && zhanfaJuli >= Math.abs(index - i);
        }
        return zhanfaJuli >= Math.abs(index - i);
      }
      return item.lr !== u.lr && zhanfaJuli >= Math.abs(index - i);
    }
  });
};

// 数组随机元素
export const randomItem = (list: any[]) => {
  return list[Math.floor(Math.random() * list.length)];
};

// 数组随机元素
export const randomWujiang = (arr: any, count: any) => {
  // 检查数组长度是否小于等于指定的count
  if (arr.length <= count) {
    return arr;
  }
  let selected: any = [];
  while (selected.length < count) {
    const randomIndex = Math.floor(Math.random() * arr.length);
    if (!selected.includes(arr[randomIndex])) {
      selected.push(arr[randomIndex]);
    }
  }
  return selected;
};
